//
//  JCMessageTextContent.swift
//  JChat
//
//  Created by JIGUANG on 2017/3/9.
//  Copyright © 2017年 HXHG. All rights reserved.
//

import UIKit
import MTCategory

open class JCRedPackTipContent: NSObject, JCMessageContentType {
    public weak var delegate: JCMessageDelegate?
    public override init() {
        let text = "this is a test text"
        self.text = NSAttributedString(string: text)
        super.init()
    }
    public init(text: String) {
        self.text = NSAttributedString(string: text)
        super.init()
    }
    public init(attributedText: NSAttributedString) {
        self.text = attributedText
        super.init()
    }
    
    public init(_ infos: [String : Any]) {
        self.text = NSAttributedString(string: "")
        
        var textString = ""
        if let senderUserName: String = infos["senderUserName"] as? String,
            let pullNickname: String = infos["pullNickname"] as? String,
            let senderNickname: String = infos["senderNickname"] as? String {
            
            let isSelf: Bool = (senderUserName == JMSGUser.myInfo().username)
            /// 如果是自己发的红包
            if isSelf {
                if let pullUserName = infos["pullUserName"] as? String {
                    /// 自己领取了自己发的红包
                    if senderUserName == pullUserName {
                        textString = " 你领取了自己发的红包"
                    } else {
                        textString = " \(pullNickname)领取了你的红包"
                    }
                } else {
                   textString = " \(pullNickname)领取了你的红包"
                }
            } else {
                /// 不是自己发的红包
                if let pullUserName = infos["pullUserName"] as? String {
                    /// 不是自己领取的
                    if JMSGUser.myInfo().username == pullUserName {
                        textString = " 你领取了\(senderNickname)的红包"
                    } else {
                        textString = ""
                    }
                } else {
                    
                }
            }
        }
        
        let attribute: NSMutableAttributedString = NSMutableAttributedString(string: textString)
        if textString.count > 0 {
            let pstyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
            pstyle.alignment = .center
            attribute.addAttributes([NSAttributedString.Key.font : PingFangSCRegularFont(12),
                                      NSAttributedString.Key.foregroundColor : UIColor(hex: 0x999999),
                                      NSAttributedString.Key.paragraphStyle : pstyle,
                                      NSAttributedString.Key.kern : 1],
                                     range: NSRange(location: 0, length: textString.count))
            
            let attachText = NSMutableAttributedString.attachmentString(withContent: UIImage(named: "club_redpacket_new"), contentMode: .scaleAspectFit, attachmentSize: CGSize(width: 15, height: 18), alignTo: PingFangSCRegularFont(12), alignment: .top)
            
            attachText.alignment = .center
            
            attribute.insert(attachText, at: 0)
        }
        
        self.text = attribute
        
        self.infos = infos
        super.init()
    }
    
    open class var viewType: JCMessageContentViewType.Type {
        return JCRedPackTipContentView.self
    }
    open var layoutMargins: UIEdgeInsets = .init(top: 9, left: 0, bottom: 9, right: 0)
    
    open var text: NSAttributedString
    
    open var infos: [String : Any]?
    
    open func sizeThatFits(_ size: CGSize) -> CGSize {
//        let mattr = NSMutableAttributedString(attributedString: text)
//        mattr.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 16), range: NSMakeRange(0, mattr.length))
//
//        let mattrSize = mattr.boundingRect(with: CGSize(width: 220.0, height: Double(MAXFLOAT)), options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil)
//        self.text = mattr
//        let attr = NSMutableAttributedString(attributedString: self.text)
//
//        let mattrSize = attr.boundingRect(with: CGSize(width: 250.0, height: Double(MAXFLOAT)), options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil)
//        let size = CGSize(width: mattrSize.size.width + 11 + 20, height: mattrSize.size.height + 4)
//
//
//        return size
        
        if self.text.string.count == 0 {
            return .zero
        }
        
        
        let layout: YYTextLayout = YYTextLayout(containerSize: CGSize(width: 250.0, height: CGFloat.greatestFiniteMagnitude) , text: self.text)!
        
        return CGSize(width: layout.textBoundingSize.width - 36 + 1 * CGFloat(self.text.string.count), height: layout.textBoundingSize.height + 2)
        
//        return .init(width: ScreenWidth - 50, height: 20)
    }
}
